/**
 * Created by hehe on 2015/7/21.
 */
var BizFactory = require('../biz'),
    BaseAction = require('./BaseAction').BaseAction,
    childProcess = require('child_process'),
    isRuning = false;

exports.beginParseExcel = function (req, res) {
    var baseAction = new BaseAction();
    if(isRuning){
        return res.send(baseAction.formattedResult(false, '正在解析，请稍候...'));
    }
    isRuning = true;
    var n = childProcess.fork('../action/process/executeExcelParse.js');
    n.on('message', function(msg) {
        isRuning = false;
        if(msg.error){
            res.send(baseAction.formattedResult(false, msg.error));
        }else{
            res.send(baseAction.formattedResult(true, msg));
        }
    });
    n.send({});
}

//exports.beginParseExcel = function (req, res) {
//    var baseAction = new BaseAction();
//    var orderBiz = new BizFactory.OrderBiz();
//
//    orderBiz.excelInsertedIntoDB();
//
//}

exports.findOrderByConditions = function (req, res) {
    var baseAction = new BaseAction(), params = req.query;

    params.conditions = {
        channelsOrderNo: params.channelsOrderNo,
        expressRecipient: params.expressRecipient,
        expressRecipientTel: params.expressRecipientTel,
        sn: params.sn,
        receiveAddress: params.receiveAddress,
        takeAwayTime: params.takeAwayTime,
        batchID:params.batchID
    }



    if(params.conditions.exWareHouseBeginTime){
        params.conditions.exWareHouseBeginTime =  new Date(params.exWareHouseBeginTime);
    }

    if(params.conditions.exWareHouseEndTime){
        params.conditions.exWareHouseEndTime =  new Date(params.exWareHouseEndTime);
    }

    params.pageInfo = {
        pageSize: params.pageSize || 20,
        startAt: params.startAt || 0
    }
    var promise = new BizFactory.OrderBiz().findOrderByConditions(params);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.exportExcel = function(req,res){
    var baseAction = new BaseAction(), params = req.query;
    params.conditions = {

    }

    if(params.conditions.exWareHouseBeginTime){
        params.conditions.exWareHouseBeginTime =  new Date(params.exWareHouseBeginTime);
    }

    if(params.conditions.exWareHouseEndTime){
        params.conditions.exWareHouseEndTime =  new Date(params.exWareHouseEndTime);
    }

    params.pageInfo = {
        pageSize: params.pageSize || 200,
        startAt: params.startAt || 0
    }
    var promise = new BizFactory.OrderBiz().exportExcel(params);
    promise.then(function (result) {

        res.setHeader('Content-Type', 'application/vnd.openxmlformats');
        res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
        res.end(result, 'binary');
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.doubleChecked = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().doubleChecked(req.query.batchID);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, '审核成功'));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.updateOrder = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().updateOrder(req.body);
    promise.then(function (result) {
        console.log(result)
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.deleteOrder = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().deleteOrder(req.query.orderID);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.deleteOrderByBatchID = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().deleteOrderByBatchID(req.query.batchID);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.findOrderInfoByOrderID = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().findOrderInfoByOrderID(req.query.orderID);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.exWareHouse = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().exWareHouse(req.body);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}

exports.inWareHouse = function(req,res){
    var baseAction = new BaseAction();
    var promise = new BizFactory.OrderBiz().inWareHouse(req.body);
    promise.then(function (result) {
        res.send(baseAction.formattedResult(true, result));
    }, function (err) {
        res.send(baseAction.formattedResult(false, err));
    });
}